<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Redirect, Input;
use App\Land;
use App\Contract;
use Illuminate\Support\Facades\DB;

class LandController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    //
    public function index()  
	{
	     $lands = DB::table('lands')
            ->join('contracts', 'contracts.code', '=', 'lands.contract_code')
            ->join('landowners', 'landowners.id', '=', 'contracts.landowner_id')
            ->select('lands.*', 'landowners.name as loname')
            ->get();

        return view('land.index')->withLands($lands);
	}

    public function all()  
    {
        $lands = DB::table('lands')
            ->join('contracts', 'contracts.code', '=', 'lands.contract_code')
            ->join('landowners', 'landowners.id', '=', 'contracts.landowner_id')
            ->select('lands.*', 'landowners.name as loname')
            ->get();

        return response()->json($lands); 
    }

    //
    public function showByLandowner($landowner_id)  
    {
        $lands = DB::table('lands')
            ->join('contracts', 'contracts.code', '=', 'lands.contract_code')
            ->join('landowners', 'landowners.id', '=', 'contracts.landowner_id')
            ->select('lands.*', 'landowners.name as loname')
            ->where('contracts.landowner_id', $landowner_id)
            ->get();

         return view('land.index')->withLands($lands);
     
    }

    public function showByContract($contract_id)  
    {
        $lands = DB::table('lands')
            ->join('contracts', 'contracts.code', '=', 'lands.contract_code')
            ->join('landowners', 'landowners.id', '=', 'contracts.landowner_id')
            ->select('lands.*', 'landowners.name as loname')
            ->where('contracts.id', $contract_id)
            ->get();

         return view('land.index')->withLands($lands);
     
    }

    public function search($team = null,$name = null)  
    {
        $sql = "select lands.*,landowners.name as loname from lands,contracts,landowners where landowners.id = contracts.landowner_id and contracts.code=lands.contract_code";

        if (isset($team) && $team != "all"){
            $sql = $sql." and landowners.team='".$team."'";
        }
        
        if (isset($name)){
            $sql = $sql." and landowners.name like '%".$name."%'";
        }
     
        // return $sql;
        $lands = DB::select($sql);
        // $lands = DB::table('lands')
        //     ->join('contracts', 'contracts.code', '=', 'lands.contract_code')
        //     ->join('landowners', 'landowners.id', '=', 'contracts.landowner_id')
        //     ->select('lands.*', 'landowners.name as loname')
        //     ->where("landowners.team", $team)
        //     // ->where('landowners.name', 'like', '%' . $name . '%')
        //     ->get(); 

         return view('land.search')->withLands($lands);
     
    }

	public function create()
    {
        $contracts = Contract::all();
        return view('land.create')->withContracts($contracts);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:lands',
            'area' => 'required',
        ]);

        $land = new Land;
        $land->name = $request->input("name");
        $land->contract_code = $request->input("contract_code");
        $land->area = $request->input("area");
        $land->price = $request->input("price");
 		$land->location = $request->input("location");
		$land->catalog = $request->input("catalog");
        $land->comment = $request->input("comment");
        $land->amount = $land->area*$land->price;

     
        if ($land->save()) {
            return Redirect::to('land');
        } else {
            return Redirect::back()->withInput()->withErrors('保存失败！');
        }

    }

    public function modify($id)
    {
        $contracts = Contract::all();
   
        $land = DB::table('lands')->where('id', $id)->first();
    
        return view('land.modify', ["contracts"=>$contracts,"land"=>$land]);
        // return response()->json($landowner); 
    }

    public function update(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'id' => 'required',
        ]);

 
        $land = [
        "name"=>$request->input("name"),
        "catalog"=>$request->input("catalog"),
        "contract_code"=>$request->input("contract_code"),
        "area"=>$request->input("area"),
        "price"=>$request->input("price"),
        "location"=>$request->input("location"),
        "comment"=>$request->input("comment"),
        "amount"=> $request->input("area")*$request->input("price")
        ];
    
        DB::table('lands')
            ->where('id', $request->input("id"))
            ->update($land);
      
        return Redirect::to('land');
    }

    public function delete($id)
    {
        DB::table('lands')->where('id', $id)->delete();
     
        return Redirect::to('land');
     }
}
